/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.txweb.view;

import com.jspx.boot.environment.Environment;
import com.jspx.sioc.annotation.Ref;
import com.jspx.txweb.annotation.HttpMethod;
import com.jspx.txweb.annotation.Safety;
import com.jspx.txweb.annotation.TurnPage;
import com.jspx.txweb.dao.MemberDAO;
import com.jspx.txweb.dao.PermissionDAO;
import com.jspx.txweb.env.TXWebIoc;
import com.jspx.txweb.support.ActionSupport;
import com.jspx.txweb.table.MemberRole;
import com.jspx.txweb.table.UserSession;
import com.jspx.utils.StringUtil;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: chenyuan
 * Date: 12-12-10
 * Time: 下午3:27
 */
@HttpMethod(caption = "在线人员")
public class OnlineView extends ActionSupport {
    public OnlineView() {

    }

    private int count = 0; //a page show row
    private int currentPage = 1; //currently page number
    private String sort = "createDate:D,replyDate:D"; //sort:use ssql to look com.jspx.sober.ssql.SSqlExpression
    private String term = StringUtil.empty;  //term:use ssql to look com.jspx.sober.ssql.SSqlExpression

    protected MemberDAO memberDAO;
    @Ref(name = TXWebIoc.memberDAO, namespace = TXWebIoc.userNamespace)
    public void setMemberDAO(MemberDAO memberDAO) {
        this.memberDAO = memberDAO;
    }

    protected PermissionDAO permissionDAO;
    @Ref(name = TXWebIoc.permissionDAO)
    public void setPermissionDAO(PermissionDAO permissionDAO) {
        this.permissionDAO = permissionDAO;
    }


    public int getCount() {
        if (count <= 0) {
            count = config.getInt(Environment.rowCount, 18);
        }
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public String getSort() {
        return sort;
    }

    @Safety(empty = false)
    public void setSort(String sort) {
        this.sort = sort;
    }


    public String getTerm() {
        return term;
    }

    @Safety(empty = false)
    public void setTerm(String term) {
        this.term = term;
    }

    public List<UserSession> getOnlineList() throws Exception {
        List<UserSession> list = memberDAO.getOnlineList(term, sort, getCurrentPage(), getCount(), true);
        for (UserSession userSession : list) {
            if (userSession.isGuest()) {
                userSession.setRole(permissionDAO.getRole(config.getInt(Environment.guestRole)));
            } else {
                List<MemberRole> roles = permissionDAO.getMemberRoles(userSession.getUid());
                if (!roles.isEmpty()) {
                    for (MemberRole memberRole : roles) {
                        userSession.setRole(memberRole.getRole());
                    }
                } else
                    userSession.setRole(permissionDAO.getRole(config.getInt(Environment.registerRole)));
            }
        }
        return list;
    }


    /**
     * @return 得到留言总数
     * @throws Exception
     */
    public int getTotalCount() throws Exception {
        return memberDAO.getOnlineCount(term);
    }

    private String turnPage = StringUtil.empty;

    @Safety(request = false)
    @TurnPage(params = "term;sort", enable = "true")
    public void setTurnPage(String turnPage) {
        this.turnPage = turnPage;
    }

    public String getTurnPage() {
        return turnPage;
    }

}